package com.boomsecret.concurrent;

import java.util.concurrent.*;

/**
 * Created by wangjinliang on 2018/1/23.
 */
public class ScheduledThreadPoolExecutorDemo {
//    public static void main(String[] args) {
//        ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1);
//        executor.execute(() -> {
//            System.out.println("execute");
//        });
//        executor.shutdown();
//    }

//    public static void main(String[] args) {
//        ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1);
//        executor.schedule(() -> {
//            System.out.println("schedule Runnable");
//        }, 2, TimeUnit.SECONDS);
//        executor.shutdown();
//    }

//    public static void main(String[] args) {
//        ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1);
//        executor.schedule(new Callable<Object>() {
//            @Override
//            public Object call() throws Exception {
//                System.out.println("schedule Callable");
//                return null;
//            }
//        }, 2, TimeUnit.SECONDS);
//        executor.shutdown();
//    }

//    public static void main(String[] args) {
//        ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1);
//        long startTime = System.currentTimeMillis() / 1000;
//        executor.scheduleAtFixedRate(()->{
//            try {
//                long endTime = System.currentTimeMillis() / 1000;
//                System.out.println("time:" + (endTime - startTime));
//                Thread.sleep(1000);
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        }, 1, 2, TimeUnit.SECONDS);
//    }

//    public static void main(String[] args) {
//        ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1);
//        long startTime = System.currentTimeMillis() / 1000;
//        executor.scheduleWithFixedDelay(()->{
//            try {
//                long endTime = System.currentTimeMillis() / 1000;
//                System.out.println("time:" + (endTime - startTime));
//                Thread.sleep(1000);
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        }, 1, 2, TimeUnit.SECONDS);
//    }

    public static void main(String[] args) throws Exception {
        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);
        long startTime = System.currentTimeMillis() / 1000;
        scheduledExecutorService.scheduleWithFixedDelay(()->{
            try {
                long endTime = System.currentTimeMillis() / 1000;
                System.out.println("time:" + (endTime - startTime));
                Thread.sleep(1000);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }, 1, 2, TimeUnit.SECONDS);
        Thread.sleep(10 * 1000);
        scheduledExecutorService.shutdown();
    }
}
